package com.amkai.mobile.common.job.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

import com.amkai.mobile.common.job.MobileJob;
import com.amkai.mobile.common.job.login.MobileLoginJob;



public class ByteSerializer {
	
	public static Object deserializeObject(String data) { 
	    try { 
	      byte[] b = Base64.decode(data);	 
	      ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(b)); 
	      Object object = in.readObject(); 
	      in.close(); 
	 
	      return object; 
	    } catch(ClassNotFoundException cnfe) { 
	       
	 
	      return null; 
	    } catch(IOException ioe) { 
	  
	 
	      return null; 
	    } 
	  } 
	
	
	public static String serializeObject(Object o) { 
	    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
	    try { 
	      ObjectOutput out = new ObjectOutputStream(bos); 
	      out.writeObject(o); 
	      out.close(); 
	      byte[] buf = bos.toByteArray();      
	      return Base64.encode(buf); 
	    } catch(IOException ioe) { 	       	 
	      return null; 
	    } 
	  } 
	
	
	public static void main(String[] args) {
		MobileLoginJob mlj = new MobileLoginJob();
		mlj.userName = ("Teszt Elek");
		String stringData = serializeObject(mlj);
		System.out.println(stringData);
		MobileJob mj = (MobileJob) deserializeObject(stringData);
		if (mj instanceof MobileLoginJob){
			System.out.println(((MobileLoginJob)mj).userName);
		}
		
	}


}
